package org.springframework.amqp.tutorials.rabbitmq_topics.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.tutorials.rabbitmq_topics.mq.Tut5Receiver;
import org.springframework.amqp.tutorials.rabbitmq_topics.mq.Tut5Sender;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile({"tut5", "topics"})
@Configuration
public class Tut5Config {

    @Bean
    public TopicExchange topic() {
        return new TopicExchange("tut.topic");
    }

    @Profile("sender")
    @Bean
    public Tut5Sender sender() {
        return new Tut5Sender();
    }

    @Profile("receiver")
    private static class ReceiverConfig {
        @Bean
        public Tut5Receiver receiver() {
            return new Tut5Receiver();
        }

        @Bean
        public Queue autoDetectQueue1() {
            return new AnonymousQueue();
        }

        @Bean
        public Queue autoDetectQueue2() {
            return new AnonymousQueue();
        }

        @Bean
        public Binding binding1a(TopicExchange topicExchange, Queue autoDetectQueue1) {
            return BindingBuilder
                    .bind(autoDetectQueue1)
                    .to(topicExchange)
                    .with("*.orange.*");
        }

        @Bean
        public Binding binding2a(TopicExchange topicExchange, Queue autoDetectQueue2) {
            return BindingBuilder
                    .bind(autoDetectQueue2)
                    .to(topicExchange)
                    .with("*.*.rabbit");
        }

        @Bean
        public Binding binding2b(TopicExchange topicExchange, Queue autoDetectQueue2) {
            return BindingBuilder
                    .bind(autoDetectQueue2)
                    .to(topicExchange)
                    .with("lazy.#");
        }
    }
}
